﻿using ML.Entity;
using ML.IRepository;
using ML.Repository;
using ML.Utils;
using System;
using System.Linq;

namespace ML.Business
{
    /// <summary>
    /// 所有定时任务的实现统一放在这里实现
    /// </summary>
    public class QuartzBusiness
    {
        private readonly ILogger logger;
        private readonly ICapitalAccountRepository capitalAccountRepository;
        private readonly IOrderRepository orderRepository;
        private readonly IProductRepository productRepository;
        private readonly IOrderDetailsRepository orderDetailsRepository;
        public QuartzBusiness()
        {
            logger = new NLogLogger();
            capitalAccountRepository = new CapitalAccountRepository();
            orderRepository = new OrderRepository();
            productRepository = new ProductRepository();
            orderDetailsRepository = new OrderDetailsRepository();
        }

        /// <summary>
        /// 资金账户数据的定时执行
        /// </summary>
        /// <returns></returns>
        public BaseResult<bool> TimeCollectMerchantAccount()
        {
            //查询所有未删除的商户资金信息(ML_CapitalAccount);
            var lists = capitalAccountRepository.GetCollectInfo("-1");
            logger.Info("开始执行(统计商户资金账户数据定时开始)：定时总共处理了(" + lists.Count() + ")条数据，循环处理开始");
            if (lists.Count() <= 0)
            {
                return new BaseResult<bool>(202, false);
            }

            //循环商户信息进行处理—根据商户Id查询商户所有订单的额度，查询之后写入资金账户的商户总金额(Balance)字段
            foreach (var list in lists)
            {
                //根据商户Id查询商户下所有订单的总金额
                var data = orderRepository.GetTrueOrderInfoByMerchantId(list.MerchantId);
                if (data.Count() < 1)
                {
                    logger.Info("商户Id为：" + list.MerchantId + "商户未查询到订单信息，不进行汇总");
                    continue;
                }

                decimal actPayPrice = data.Sum(c => c.ActPayRMB);
                var isTrue = capitalAccountRepository.UpdateBalance(list.MerchantId, actPayPrice);
                if (isTrue)
                {
                    logger.Info("商户Id为：" + list.MerchantId + "执行成功");
                }
                else
                {
                    logger.Info("商户Id为：" + list.MerchantId + "执行失败");
                }
            }
            logger.Info("结束执行：(统计商户资金账户数据定时结束)");
            return new BaseResult<bool>(200, true);
        }

        /// <summary>
        /// 产品销量定时扫描汇总
        /// </summary>
        /// <returns></returns>
        public BaseResult<bool> TimeCollectProductSalesVolume()
        {
            //查询所有未删除的产品信息进行处理(ML_Product)
            var lists = productRepository.GetProductInfoList();
            logger.Info("开始执行(产品销量数据定时开始)：定时总共处理了(" + lists.Count() + ")条数据，循环处理开始");
            if (lists.Count() <= 0)
            {
                return new BaseResult<bool>(202, false);
            }

            //循环产品信息进行处理—根据产品Id查询产品销售情况，查询之后写入产品表中的销量字段
            foreach (var list in lists)
            {
                //根据产品Id查询产品销售情况的总数
                var data = orderDetailsRepository.GetOrderDetailByProductId(list.Id);
                if (data.Count() < 1)
                {
                    logger.Info("产品Id为：" + list.Id + "产品未查询到销量信息，不进行汇总");
                    continue;
                }
                int productNumber = data.Sum(c => c.ProductNumber);
                var isTrue = productRepository.UpdateSalesVolume(list.Id, productNumber);
                if (isTrue)
                {
                    logger.Info("产品Id为：" + list.Id + "执行成功");
                }
                else
                {
                    logger.Info("产品Id为：" + list.Id + "执行失败");
                }
            }
            logger.Info("结束执行(产品销量数据定时结束)");
            return new BaseResult<bool>(200, true);
        }

        /// <summary>
        /// 订单完成定时扫描
        /// </summary>
        /// <returns></returns>
        public BaseResult<bool> TimeCollectOrderState()
        {
            //查询所有已支付已接单_昨天的订单
            var lists = orderRepository.GetOrderInfoListDays();
            logger.Info("开始执行(定时扫描已支付已接单的昨天的订单开始)：定时总共处理了(" + lists.Count() + ")条数据，循环处理开始");
            if (lists.Count() <= 0)
            {
                return new BaseResult<bool>(202, false);
            }

            //循环订单信息进行处理—根据订单Id修改订单的状态为订单已完成
            foreach (var list in lists)
            {
                var isTrue = orderRepository.UpdateState(list.Id, (int)OrderStatus.finish);
                if (isTrue)
                {
                    logger.Info("订单Id为：" + list.Id + "流水号为："+list.OrderNumber+"，执行成功");
                }
                else
                {
                    logger.Info("订单Id为：" + list.Id + "流水号为："+list.OrderNumber+"，执行失败");
                }
            }
            logger.Info("结束执行：(定时扫描已支付已接单的昨天的结束)");
            return new BaseResult<bool>(200, true);
        }

        /// <summary>
        /// 定时取消当天从00:00开始到当前时间的前指定分钟未支付的订单
        /// </summary>
        /// <returns></returns>
        public BaseResult<bool> TimeCollectOrderCancel()
        {
            //查询当天从00:00开始到当前时间的前指定分钟未支付的订单信息
            var beginTime = DateTime.Now.Date;
            var endTime = DateTime.Now.AddMinutes(-ConfigHelper.OrderCancelTime);
            var lists = orderRepository.GetOrderUnPay(beginTime,endTime);
            logger.Info("开始执行(定时扫描当天"+ConfigHelper.OrderCancelTime+"分钟以前未支付订单)：定时总共处理了(" + lists.Count() + ")条数据，循环处理开始");
            if (lists.Count() <= 0)
            {
                return new BaseResult<bool>(202, false);
            }

            //循环订单信息进行处理—根据订单Id修改订单的状态为订单已完成
            foreach (var list in lists)
            {
                var isTrue = orderRepository.UpdateState(list.Id, (int)OrderStatus.systemcancel);
                if (isTrue)
                {
                    logger.Info("订单Id为：" + list.Id + "流水号为：" + list.OrderNumber + "，执行成功");
                }
                else
                {
                    logger.Info("订单Id为：" + list.Id + "流水号为：" + list.OrderNumber + "，执行失败");
                }
            }
            logger.Info("结束执行：(定时扫描当天" + ConfigHelper.OrderCancelTime + "分钟以前未支付订单)");
            return new BaseResult<bool>(200, true);
        }
    }
}